(function($, window, undefined) {
    $.EtValid = $.EtValid || {};
	$.EtValid.list = {};
	
	$.extend($.EtValid, {
        "version": "1.0.0",
		//定义外部扩展方法接口
        "extend": function(methods) {
            $.extend($.fn.EtValid, methods);
			
			if (!this.no_legacy_api) {
				$.fn.extend(methods);
			}
        },
		"randomString" : function(length) {
			var chars = 'abcdefghiklmnopqrstuvwxyz'.split('');
			 
			if (! length) {
				 length = Math.floor(Math.random() * chars.length);
			 }
			 
			var str = '';
			 for (var i = 0; i < length; i++) {
				 str += chars[Math.floor(Math.random() * chars.length)];
			 }
			 return str;
		 }
    });
	
	//常用扩展方法
	$.EtValid.extend({
		
    });
	
	
	$.fn.EtValid = function(options) {
        var _this = this;

		//合并参数
        var config = $.extend({},$.fn.EtValid.defaults, options);
        
		_this.config = config;
		
		//初始化
		init(_this);
		
		if(_this.config.objId){
		}else{
			_this.config.objId = $.EtValid.randomString(8);
		}
		
		$.EtValid.list[_this.config.objId] = _this;
				
		return _this;
    };
	
	$.fn.EtValid.defaults = {
		checkStatus: 'submit', //支持focusin、focusout、submit/change等多种
		datatype: {}
    };
	
	var init = function(_this){
		var _that = _this;
		
		var datatype = {
			"match":/^(.+?)(\d+)-(\d+)$/,
			"*":/[\w\W]+/,
			"*6-16":/^[\w\W]{6,16}$/,
			"n":/^\d+$/,
			"n6-16":/^\d{6,16}$/,
			"s":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]+$/,
			"s6-18":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$/,
			"p":/^[0-9]{6}$/,
			"m":/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/,
			"e":/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
			"url":/^(\w+:\/\/)?\w+(\.\w+)+.*$/
		};
		
		jQuery.extend(_that.config.datatype, datatype); //合并参数

		bindEvent(_that);
	};
	
	//事件绑定
	function bindEvent(_this){
		var _that = _this;
		
		//绑定验证
		_that.find('.etvalid').each(function(index, element) {
			var checkevent = $(this).attr('checkevent');  //检测事件
			
			//表单元素自身触发验证
			if(checkevent=='click' || checkevent=='focusin' || checkevent=='focusout' || checkevent=='change'){
				$(this).live(checkevent,function(){
					_that.validObj = $(this);
					checkData(_that);
				});
			}
		});
		
		//绑定提交
		_that.find(_that.config.submitSelector).on('click',function(){
			//全部检查
			var result = {};
			result.err = 0;
			_that.find('.etvalid').each(function(index, element) {
				_that.validObj = $(this);
				if(!checkData(_that)){
					result.err++;
				}
			});
			
			if(!result.err){
				if(!_that.config.dealSubmitMy){
					_that.submit();
				}else{
					_that.config.dealSubmitOk(result);
				}
			}else{
				_that.config.dealSubmitErr(result);
			}	
		});
	}
	
	//显示错误
	
	//数据校验
	function checkData(_this){
		var _that = _this;
		var validResult = false;
		
		//验证对象处理
		var msginfo = _that.validObj.attr('msginfo'); //默认提示
		var msgerr = _that.validObj.attr('msgerr'); //错误提示
		var msgok = _that.validObj.attr('msgok'); //正确提示
		var datatype = _that.validObj.attr('datatype');  //数据类型
		var nowvalue = _that.validObj.val();  //当前值
		var tisobj = _that.validObj.parents('.ele_content').next('.ele_tis').find('div');
		
		
		if(datatype.indexOf('radio:')==0){
			var rule = datatype.replace('radio:','');
			if(rule == 'notempty'){
				var name = _that.validObj.attr('name');
				_that.find('input[name="'+name+'"]').each(function(i,n){
					if($(this).prop('checked')){
						validResult = true;
						return false;
					}
				});
			}else{
				validResult = true;
			}
			
		}else if(datatype.indexOf('checkbox:')==0){
			var rule = datatype.replace('checkbox:','');
			var name = _that.validObj.attr('cname');
			
			if(rule == 'notempty'){				
				_that.find('input[cname="'+name+'"]').each(function(i,n){
					if($(this).prop('checked')){
						validResult = true;
						return false;
					}
					
				});
			}else{
				var num = 0;
				_that.find('input[cname="'+name+'"]').each(function(i,n){
					if($(this).prop('checked')){
						num++;
					}
				});
				if(rule.indexOf('min:')==0){
					var len = rule.replace('min:','');
					if(parseInt(len) > num){
						validResult = true;
					}
				}else if(rule.indexOf('max:')==0){
					var len = rule.replace('max:','');					
					if(parseInt(len) < num){
						validResult = true;
					}
				}else if(rule.indexOf('both:')==0){
					var str = rule.replace('both:','');	
					var arr = str.split(',');		
					if(parseInt(arr[0]) < num && parseInt(arr[1]) > num){
						validResult = true;
					}
				}else{
					validResult = true;
				}
			}
		}else if(datatype.indexOf('select:')==0){
			var rule = datatype.replace('select:','');
			if(rule == 'notempty'){
				if(_that.validObj.val()){
					validResult = true;
				}
			}else{
				validResult = true;
			}
		}else{
			//正则处理
			var regular;	
			if(datatype.indexOf('regx:')==0){							
				regular = eval(datatype.replace('regx:',''));
			}
				
			if(datatype in _that.config.datatype){
				regular = _that.config.datatype[datatype];
			}
			
			var validResult = regular.test(nowvalue);
		}
		
		if(validResult){
			tisobj.html(msgok);
			tisobj.removeClass('info').removeClass('err').addClass('ok');
		}else{
			tisobj.html(msgerr);
			tisobj.removeClass('info').removeClass('ok').addClass('err');
		}
		
		return validResult;
	}
})(jQuery, window);